package com.deer.wms.system.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.RedisUtil;
import com.deer.wms.system.manage.constant.SysParamConstant;
import com.deer.wms.system.manage.dao.SysParamMapper;
import com.deer.wms.system.manage.model.*;
import com.deer.wms.system.manage.service.SysParamDetailService;
import com.deer.wms.system.manage.service.SysParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by guo on 2019/07/12.
 */
@Service
@Transactional
public class SysParamServiceImpl extends ServiceImpl<SysParamMapper, SysParam> implements SysParamService {

    @Autowired
    private SysParamMapper sysParamMapper;

    @Autowired
    private SysParamDetailService sysParamDetailService;

    @Autowired
    private SysParamService sysParamService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<SysParamDto> findList(SysParamCriteria criteria) {
        return sysParamMapper.findList(criteria);
    }

    public List<SysParamDto> reSetRedis(){
        SysParamCriteria criteria = new SysParamCriteria();
        List<SysParamDto> list = findList(criteria);

        SysParamConstant.paramValues.clear();
        for (SysParamDto ysParamDto:list) {
            SysParamConstant.paramValues.put(ysParamDto.getParamName(),ysParamDto.getValue());
            SysParamDetailCriteria sysParamDetailCriteria = new SysParamDetailCriteria();
            sysParamDetailCriteria.setSysParamId(ysParamDto.getSysParamId());
            List<SysParamDetail> detailList = sysParamDetailService.findList(sysParamDetailCriteria);
            ysParamDto.setDetails(detailList);
        }
        redisUtil.set("sysParamDtos",list);
        Object object = redisUtil.get("sysParamDtos");
        return   (List<SysParamDto>)object;
    }

    @Override
    public Result createSysParam(SysParam sysParam) {
        List<SysParamDto> list = sysParamService.findByCode(sysParam.getParamCode());
        List<SysParamDto> list1 = sysParamService.findByName(sysParam.getParamName());

        if (list.size() == 0 && list1.size() == 0) {
            save(sysParam);
            return ResultGenerator.genSuccessResult();
        } else if (list.size() == 0 && list1.size() != 0) {
            throw new ServiceException(CommonCode.SYSPARAM_EXISTS, "此系统参数已存在");
        } else{
            throw new ServiceException(CommonCode.SYSPARAM_CODE_EXISTS, "此系统参数编码已存在");

        }
    }

    @Override
    public List<SysParamDto> findByCode(String paramCode){
        return sysParamMapper.findByCode(paramCode);
    }

    @Override
    public List<SysParamDto> findByName(String paramName){
        return sysParamMapper.findByName(paramName);
    }

    @Override
    public List<SysParam> findByMemo(String memo) {
        return sysParamMapper.findByMemo(memo);
    }

    @Override
    public SysParam findBy(String field, String value) {
        QueryWrapper<SysParam> qw = new QueryWrapper<>();
        return getOne(qw.eq(field, value));
    }

    public void update(SysParam sysParam){

        redisUtil.remove(sysParam.getMemo()+"memo");
        redisUtil.remove(sysParam.getModuleCode()+"memo");
        redisUtil.remove(sysParam.getParamCode()+"memo");
        super.updateById(sysParam);
    }
}
